package com.kevin.permission.util;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.kevin.permission.exception.ParamsException;
import org.apache.commons.collections.MapUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.*;

public class BeanValidator {

    private static ValidatorFactory validatorFactory= Validation.buildDefaultValidatorFactory();

    private static <T> Map<String,String> validate(T t,Class... groups){
        Validator validator=validatorFactory.getValidator();
        Set validateResult=validator.validate(t,groups);
        if (validateResult.isEmpty()){
            return Collections.emptyMap();
        }else{
            LinkedHashMap errors= Maps.newLinkedHashMap();
            Iterator iterator=validateResult.iterator();
            while (iterator.hasNext()){
                ConstraintViolation violation= (ConstraintViolation) iterator.next();
                errors.put(violation.getPropertyPath().toString(),violation.getMessage());
            }
            return errors;
        }
    }

    private static Map<String,String> validateList(Collection<?> collection){
        Preconditions.checkNotNull(collection);
        Iterator iterator=collection.iterator();
        Map error;

        do{
            if (!iterator.hasNext()){
                return Collections.emptyMap();
            }
            Object object=iterator.hasNext();
            error=validate(object,new Class[0]);
        }while(error.isEmpty());

        return error;
    }

    public static Map<String ,String> validateObject(Object first,Object... objects){
        if (objects!=null && objects.length>0){
            return validateList(Lists.asList(first,objects));
        }else{
            return validate(first,new Class[0]);
        }
    }

    public static void check(Object param){
        Map<String ,String> map=BeanValidator.validateObject(param);
        if (MapUtils.isNotEmpty(map)){
            throw  new ParamsException(map.toString());
        }
    }
}
